package com.rednote.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rednote.entity.UserViewHistory;
import com.rednote.mapper.UserViewHistoryMapper;
import com.rednote.service.UserViewHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户浏览记录表(UserViewHistory)服务实现类
 *
 * @author yao suxuan
 * @since 2025-05-24 18:10:29
 */
@Service
public class UserViewHistoryServiceImpl extends ServiceImpl<UserViewHistoryMapper, UserViewHistory> implements UserViewHistoryService {

    @Autowired
    private UserViewHistoryMapper userViewHistoryMapper;
    public Map<Integer, List<Integer>> getPostUserInteractions() {
        List<Map<String, Object>> rawList = userViewHistoryMapper.getPostUserInteractionsRaw();
        Map<Integer, List<Integer>> result = new HashMap<>();
        for (Map<String, Object> row : rawList) {
            Integer postId = (Integer) row.get("post_id");
            Integer userId = (Integer) row.get("user_id");
            result.computeIfAbsent(postId, k -> new ArrayList<>()).add(userId);
        }
        return result;
    }
}
